package br.com.automed.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

public class Util {
	
	private static ResourceBundle bundle;
	
	public static boolean isEmpty(String str, boolean trim) {
		if ( str == null || ( !trim && str.equals( "" ) ) || ( trim && str.trim().equals( "" ) ) ) {
			return true;
		}
		return false;
	}
	
	public static String getMessage(String key, Object[] params){
		
		if(bundle == null){
			FacesContext context = FacesContext.getCurrentInstance();
			UIViewRoot viewRoot = context.getViewRoot();
			Locale locale = viewRoot.getLocale();
				
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			bundle = ResourceBundle.getBundle(Constantes.PATH_MSG_PROPERTIES, locale, loader);
		}	
		
		String value = bundle.getString(key);
		if(params != null){
			MessageFormat formatter = new MessageFormat(value, bundle.getLocale());
			value = formatter.format(params);
		}
		
		
		return value;
	}
	
	public static void gerarMensagem(String msg,String idComponent){
		FacesMessage message = new FacesMessage();
		message.setSummary(msg);
		message.setSeverity(FacesMessage.SEVERITY_ERROR);
		FacesContext.getCurrentInstance().addMessage(idComponent, message);
	}
	
	public static String geraUsuario(String nome){
		
		if(nome == null){
			return null;
		}
		
		String[]vetor = nome.split(" ");
		StringBuilder builder = new StringBuilder();
		builder.append(vetor[0]);
		builder.append(".");
		builder.append(vetor[vetor.length-1]);
		
		return builder.toString();
	}
	

}
